﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ShopMall.Common
{
    /// <summary>
    /// 公用方法
    /// </summary>
    public static class ExtendClass
    {
        /// <summary>
        /// 移动电话验证
        /// </summary>
        public static bool IsMobile(this string s)
        {
            return Regex.IsMatch(s, @"^1[3458]\d{9}$");
        }

        /// <summary>
        /// Email验证
        /// </summary>
        public static bool IsEmail(this string s)
        {
            return Regex.IsMatch(s, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
        }

        /// <summary>
        /// 传真验证
        /// </summary>
        public static bool IsFax(this string s)
        {
            return Regex.IsMatch(s, @"^(86\-)?(\d{2,4}-)?([2-9]\d{6,7})+(-\d{1,4})?$");
        }

        /// <summary>
        /// 固定电话验证
        /// </summary>
        public static bool IsTel(this string s)
        {
            return Regex.IsMatch(s, @"^(0\d{2,3}-)?\d{7,8}(-\d{1,4})?$");
        }

        /// <summary>
        /// 手机或者固定电话
        /// </summary>
        public static bool IsMobileOrTel(this string s)
        {
            return Regex.IsMatch(s, @"^(1[3458]\d{9}|(0\d{2,3}-)?\d{7,8}(-\d{1,4})?)$");
        }
        /// <summary>
        /// Url地址
        /// </summary>
        /// <returns></returns>
        public static bool IsWebUrl(this string s)
        {
            return Regex.IsMatch(s, @"^(http|https|ftp):\/\/([\w-]+\.)+[\w-]+([\/\:][\w- .\/?%&=\;#\*]*)?$");
        }

        /// <summary>
        /// 替换特殊符号
        /// </summary>
        /// <param name="s">内容</param>
        /// <returns></returns>
        public static string ReplaceSymbol(this string s)
        {
            return s.Replace("&lt;", "<").Replace("&gt;", ">").Replace(@"\r", "<br />").Replace("&quot;", "\"");
        }

        /// <summary>
        /// 移除HTML标签
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string RemoveHtml(this string s)
        {
            return Regex.Replace(s, @"<[^>]*>", "");
        }

        /// <summary>
        /// 身份验证
        /// </summary>
        public static bool IsIDCard(this string s)
        {
            return Regex.IsMatch(s ?? "", @"(^\d{15}$)|(^\d{17}([0-9]|X|x)$)");
        }

        /// <summary>
        /// 判断是否是字母
        /// </summary>
        /// <param name="hzString"></param>
        /// <returns></returns>
        public static bool IsPinyin(this string s)
        {
            if (s == null || s.Trim().Length == 0) return false;
            return System.Text.RegularExpressions.Regex.IsMatch(s, @"^[a-zA-Z]+$");
        }

        /// <summary>
        /// 判断是否数字
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNumber(this string s)
        {
            return Regex.IsMatch(s ?? "", @"^\d+$");
        }

        /// <summary>
        /// 判断是否为空或者null值。
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        /// <summary>
        /// 身份验证
        /// </summary>
        public static bool ValidIDCard(this string s)
        {
            return Regex.IsMatch(s ?? "", @"(^\d{15}$)|(^\d{17}([0-9]|X|x)$)");
        }

        /// <summary>
        /// 图片格式验证
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsImg(this string s)
        {
            return Regex.IsMatch((s ?? "").ToLower(), @"(jpg|gif|png|bmp|jpeg)$");
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static string SubString(string str, int length)
        {
            return Regex.Match(str, @"^[\s\S]{0," + length + "}", RegexOptions.IgnoreCase).Value;
        }
    }
}
